#!/bin/sh
echo $0 $*
XDG_DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}

# 修改为直接获取文件夹名称的用法,不同游戏文件名不同
GAMEDIR="/mnt/SDCARD/Ports/quake3"
# GAMEDIR=${PWD}
echo ==========GAMEDIR is $GAMEDIR
controlfolder="../PortMaster"

source $controlfolder/control.txt
#source $controlfolder/tasksetter

get_controls

# 增加错误输出记录
exec > >(tee "$GAMEDIR/log.txt") 2>&1
cd $GAMEDIR

$ESUDO rm -rf ~/.q3a
ln -sfv $GAMEDIR/conf/.q3a ~/

export DEVICE_ARCH="${DEVICE_ARCH:-aarch64}"

# if [ -f "${controlfolder}/libgl_${CFW_NAME}.txt" ]; then 
#   source "${controlfolder}/libgl_${CFW_NAME}.txt"
# else
#   source "${controlfolder}/libgl_default.txt"
# fi

export LIBGL_ES=2
export LIBGL_GL=21
export LIBGL_FB=4

if [ "$LIBGL_FB" != "" ]; then
export SDL_VIDEO_GL_DRIVER="$GAMEDIR/gl4es.aarch64/libGL.so.1"
export SDL_VIDEO_EGL_DRIVER="$GAMEDIR/gl4es.aarch64/libEGL.so.1"
fi 

export LD_LIBRARY_PATH="$GAMEDIR/libs:$LD_LIBRARY_PATH"
export SDL_GAMECONTROLLERCONFIG="$sdl_controllerconfig"

$ESUDO chmod 666 /dev/tty1
$ESUDO chmod 666 /dev/uinput

$GPTOKEYB "quake3e.aarch64" -c "$GAMEDIR/quake3e.aarch64.gptk" &
./quake3e.aarch64

$ESUDO kill -9 $(pidof gptokeyb)
$ESUDO systemctl restart oga_events & 
printf "\033c" >> /dev/tty1